﻿#include "FTag.h"
#include "ui_FTag.h"

#include <QGraphicsDropShadowEffect>
#include <QStyle>
#include <QFileInfo>
#include <QFileIconProvider>
#include <QDesktopServices>
#include <QUrl>
#include <QDir>


FTag::FTag(const QString &msg, const QIcon &icon, const QColor &bgColor, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FTag)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::ToolTip);
    setAttribute(Qt::WA_TranslucentBackground, true);

    // 阴影效果
    auto* shadow = new QGraphicsDropShadowEffect(this);
    shadow->setBlurRadius(15);
    shadow->setOffset(0, 0);
    shadow->setColor(QColor(0, 0, 0, 180));
    // ui->widget->setGraphicsEffect(shadow);

    ui->iconLabel->setPixmap(icon.pixmap(ui->iconLabel->size()));
    ui->textLabel->setText(msg);

    QString qss = QString(R"(
#widget {
    background: %1;
    border-radius: 8px;
    padding: 8px 16px;
}
#widget:hover {
    background: %2;
}
)").arg(bgColor.name(), bgColor.lighter().name());
    setStyleSheet(qss);

    ui->btnClose->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarCloseButton));

    ui->widget->installEventFilter(this);
}

FTag::~FTag()
{
    delete ui;
}

void FTag::setText(const QString &text)
{
    ui->textLabel->setText(text);
}

void FTag::setIcon(const QIcon &icon)
{
    ui->iconLabel->setPixmap(icon.pixmap(ui->iconLabel->size()));
}

void FTag::on_btnClose_clicked()
{
    close();
    deleteLater();
}

bool FTag::eventFilter(QObject *watched, QEvent *e)
{
    if (watched == ui->widget) {
        if (e->type() == QEvent::MouseButtonPress) {
            m_pressed = true;
        } else if (e->type() == QEvent::MouseButtonRelease && m_pressed) {
            m_pressed = false;
            emit clicked();
        }
    }
    return false;
}


FFileTag::FFileTag(const QString &filePath, const QColor &bgColor, QWidget *parent)
    : FTag("", QIcon(), bgColor, parent)
    , m_filePath(filePath)
{
    initView();

    connect(this, &FFileTag::clicked, this, &FFileTag::onClicked);
}

void FFileTag::setFilePath(const QString &filePath)
{
    m_filePath = filePath;
    initView();
    adjustSize();
}

bool FFileTag::isFileValid() const
{
    QFileInfo info(m_filePath);
    return info.exists() && info.isFile();
}

qint64 FFileTag::fileSize() const
{
    if (!isFileValid()) return 0;
    QFile f(m_filePath);
    return f.size();
}

QString FFileTag::filename() const
{
    if (!isFileValid()) return "";
    QFileInfo info(m_filePath);
    return info.fileName();
}

QByteArray FFileTag::getFileContent()
{
    if (!isFileValid()) return "";
    QFile f(m_filePath);
    if (!f.open(QIODevice::ReadOnly)) return "";
    QByteArray con = f.readAll();
    f.close();
    return con;
}

void FFileTag::initView()
{
    if (!isFileValid()) return;
    QFileInfo info(m_filePath);

    // 获取文件类型图标
    QFileIconProvider iconProvider;
    QIcon fileIcon = iconProvider.icon(info);

    setText(info.fileName());
    setIcon(fileIcon);
    setToolTip(m_filePath);
}

void FFileTag::onClicked()
{
    if (!isFileValid()) return;
    QFileInfo info(m_filePath);
    QDesktopServices::openUrl(QUrl::fromLocalFile(info.dir().absolutePath()));
}

